﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Webcam_Filter
{
    public class Randen : Filter
    {
        protected override void runfilter(byte[] rgbValues, int hoogte, int breedte, string[] args)
        {

            int lengte = breedte * 4 * hoogte;
            byte[] copyRGB = new byte[rgbValues.Length];
            Array.Copy(rgbValues, copyRGB, rgbValues.Length);

            int breedteARGB = ((breedte) * 4);
            int offset = 128;
            int factor = 8;
            int totaalAantalPixels = breedteARGB * hoogte;

            for (int h = 0; h < totaalAantalPixels; h += 4)
            {
                int grayScale = (int)((rgbValues[h + 2] * .3) + (rgbValues[h + 1] * .59)
                + (rgbValues[h] * .11));
                copyRGB[h + 2] = (byte)grayScale;
                copyRGB[h + 1] = (byte)grayScale;
                copyRGB[h] = (byte)grayScale;
            }


            for (int h = 4; h < totaalAantalPixels - 4; h += 4)
            {
                int avg1 = (copyRGB[h + 4] + copyRGB[h + 1 + 4] + copyRGB[h + 2 + 4]) / 3;
                int avg2 = (copyRGB[h - 4] + copyRGB[h + 1 - 4] + copyRGB[h + 2 - 4]) / 3;
                int avg = factor * (avg1 - avg2) + offset;
                if (avg > 255) { avg = 255; }
                if (avg < 0) { avg = 0; }
                rgbValues[h] = (byte)avg;
                rgbValues[h + 1] = (byte)avg;
                rgbValues[h + 2] = (byte)avg;
            }
        }
    }
}